home *** CD-ROM | disk | FTP | other *** search
/ PC Answers 1998 January / PC Answers Issue 49 Cover CD January 1998.iso / LIBS / VB4 / VB4-4.CAB / calc.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-08-15  |  10.6 KB  |  346 lines

  1. VERSION 4.00
  2. Begin VB.Form Calculator 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Calculator"
  5.    ClientHeight    =   2970
  6.    ClientLeft      =   2580
  7.    ClientTop       =   1485
  8.    ClientWidth     =   3270
  9.    ClipControls    =   0   'False
  10.    BeginProperty Font 
  11.       name            =   "System"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   9.75
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    Height          =   3375
  20.    Icon            =   "CALC.frx":0000
  21.    Left            =   2520
  22.    LinkMode        =   1  'Source
  23.    LinkTopic       =   "Form1"
  24.    MaxButton       =   0   'False
  25.    ScaleHeight     =   2970
  26.    ScaleWidth      =   3270
  27.    Top             =   1140
  28.    Width           =   3390
  29.    Begin VB.CommandButton Number 
  30.       Caption         =   "7"
  31.       Height          =   480
  32.       Index           =   7
  33.       Left            =   120
  34.       TabIndex        =   7
  35.       Top             =   600
  36.       Width           =   480
  37.    End
  38.    Begin VB.CommandButton Number 
  39.       Caption         =   "8"
  40.       Height          =   480
  41.       Index           =   8
  42.       Left            =   720
  43.       TabIndex        =   8
  44.       Top             =   600
  45.       Width           =   480
  46.    End
  47.    Begin VB.CommandButton Number 
  48.       Caption         =   "9"
  49.       Height          =   480
  50.       Index           =   9
  51.       Left            =   1320
  52.       TabIndex        =   9
  53.       Top             =   600
  54.       Width           =   480
  55.    End
  56.    Begin VB.CommandButton Cancel 
  57.       Caption         =   "C"
  58.       Height          =   480
  59.       Left            =   2040
  60.       TabIndex        =   10
  61.       Top             =   600
  62.       Width           =   480
  63.    End
  64.    Begin VB.CommandButton CancelEntry 
  65.       Caption         =   "CE"
  66.       Height          =   480
  67.       Left            =   2640
  68.       TabIndex        =   11
  69.       Top             =   600
  70.       Width           =   480
  71.    End
  72.    Begin VB.CommandButton Number 
  73.       Caption         =   "4"
  74.       Height          =   480
  75.       Index           =   4
  76.       Left            =   120
  77.       TabIndex        =   4
  78.       Top             =   1200
  79.       Width           =   480
  80.    End
  81.    Begin VB.CommandButton Number 
  82.       Caption         =   "5"
  83.       Height          =   480
  84.       Index           =   5
  85.       Left            =   720
  86.       TabIndex        =   5
  87.       Top             =   1200
  88.       Width           =   480
  89.    End
  90.    Begin VB.CommandButton Number 
  91.       Caption         =   "6"
  92.       Height          =   480
  93.       Index           =   6
  94.       Left            =   1320
  95.       TabIndex        =   6
  96.       Top             =   1200
  97.       Width           =   480
  98.    End
  99.    Begin VB.CommandButton Operator 
  100.       Caption         =   "+"
  101.       Height          =   480
  102.       Index           =   1
  103.       Left            =   2040
  104.       TabIndex        =   12
  105.       Top             =   1200
  106.       Width           =   480
  107.    End
  108.    Begin VB.CommandButton Operator 
  109.       Caption         =   "-"
  110.       Height          =   480
  111.       Index           =   3
  112.       Left            =   2640
  113.       TabIndex        =   13
  114.       Top             =   1200
  115.       Width           =   480
  116.    End
  117.    Begin VB.CommandButton Number 
  118.       Caption         =   "1"
  119.       Height          =   480
  120.       Index           =   1
  121.       Left            =   120
  122.       TabIndex        =   1
  123.       Top             =   1800
  124.       Width           =   480
  125.    End
  126.    Begin VB.CommandButton Number 
  127.       Caption         =   "2"
  128.       Height          =   480
  129.       Index           =   2
  130.       Left            =   720
  131.       TabIndex        =   2
  132.       Top             =   1800
  133.       Width           =   480
  134.    End
  135.    Begin VB.CommandButton Number 
  136.       Caption         =   "3"
  137.       Height          =   480
  138.       Index           =   3
  139.       Left            =   1320
  140.       TabIndex        =   3
  141.       Top             =   1800
  142.       Width           =   480
  143.    End
  144.    Begin VB.CommandButton Operator 
  145.       Caption         =   "X"
  146.       Height          =   480
  147.       Index           =   2
  148.       Left            =   2040
  149.       TabIndex        =   14
  150.       Top             =   1800
  151.       Width           =   480
  152.    End
  153.    Begin VB.CommandButton Operator 
  154.       Caption         =   "/"
  155.       Height          =   480
  156.       Index           =   0
  157.       Left            =   2640
  158.       TabIndex        =   15
  159.       Top             =   1800
  160.       Width           =   480
  161.    End
  162.    Begin VB.CommandButton Number 
  163.       Caption         =   "0"
  164.       Height          =   480
  165.       Index           =   0
  166.       Left            =   120
  167.       TabIndex        =   0
  168.       Top             =   2400
  169.       Width           =   1080
  170.    End
  171.    Begin VB.CommandButton Decimal 
  172.       Caption         =   "."
  173.       Height          =   480
  174.       Left            =   1320
  175.       TabIndex        =   18
  176.       Top             =   2400
  177.       Width           =   480
  178.    End
  179.    Begin VB.CommandButton Operator 
  180.       Caption         =   "="
  181.       Height          =   480
  182.       Index           =   4
  183.       Left            =   2040
  184.       TabIndex        =   16
  185.       Top             =   2400
  186.       Width           =   480
  187.    End
  188.    Begin VB.CommandButton Percent 
  189.       Caption         =   "%"
  190.       Height          =   480
  191.       Left            =   2640
  192.       TabIndex        =   17
  193.       Top             =   2400
  194.       Width           =   480
  195.    End
  196.    Begin VB.Label Readout 
  197.       Alignment       =   1  'Right Justify
  198.       BackColor       =   &H0000FFFF&
  199.       BorderStyle     =   1  'Fixed Single
  200.       Caption         =   "0."
  201.       BeginProperty Font 
  202.          name            =   "MS Sans Serif"
  203.          charset         =   1
  204.          weight          =   700
  205.          size            =   12
  206.          underline       =   0   'False
  207.          italic          =   0   'False
  208.          strikethrough   =   0   'False
  209.       EndProperty
  210.       ForeColor       =   &H00000000&
  211.       Height          =   375
  212.       Left            =   120
  213.       TabIndex        =   19
  214.       Top             =   105
  215.       Width           =   3000
  216.    End
  217. Attribute VB_Name = "Calculator"
  218. Attribute VB_Creatable = False
  219. Attribute VB_Exposed = False
  220. ' ------------------------------------------------------------------------
  221. '               Copyright (C) 1994 Microsoft Corporation
  222. ' You have a royalty-free right to use, modify, reproduce and distribute
  223. ' the Sample Application Files (and/or any modified version) in any way
  224. ' you find useful, provided that you agree that Microsoft has no warranty,
  225. ' obligations or liability for any Sample Application Files.
  226. ' ------------------------------------------------------------------------
  227. Option Explicit
  228. Dim Op1, Op2                ' Previously input operand.
  229. Dim DecimalFlag As Integer  ' Decimal point present yet?
  230. Dim NumOps As Integer       ' Number of operands.
  231. Dim LastInput               ' Indicate type of last keypress event.
  232. Dim OpFlag                  ' Indicate pending operation.
  233. Dim TempReadout
  234. ' Click event procedure for C (cancel) key.
  235. ' Reset the display and initializes variables.
  236. Private Sub Cancel_Click()
  237.     ReadOut = Format(0, "0.")
  238.     Op1 = 0
  239.     Op2 = 0
  240.     Form_Load
  241. End Sub
  242. ' Click event procedure for CE (cancel entry) key.
  243. Private Sub CancelEntry_Click()
  244.     ReadOut = Format(0, "0.")
  245.     DecimalFlag = False
  246.     LastInput = "CE"
  247. End Sub
  248. ' Click event procedure for decimal point (.) key.
  249. ' If last keypress was an operator, initialize
  250. ' readout to "0." Otherwise, append a decimal
  251. ' point to the display.
  252. Private Sub Decimal_Click()
  253.     If LastInput = "NEG" Then
  254.         ReadOut = Format(0, "-0.")
  255.     ElseIf LastInput <> "NUMS" Then
  256.         ReadOut = Format(0, "0.")
  257.     End If
  258.     DecimalFlag = True
  259.     LastInput = "NUMS"
  260. End Sub
  261. ' Initialization routine for the form.
  262. ' Set all variables to initial values.
  263. Private Sub Form_Load()
  264.     DecimalFlag = False
  265.     NumOps = 0
  266.     LastInput = "NONE"
  267.     OpFlag = " "
  268.     ReadOut = Format(0, "0.")
  269.     Decimal.Caption = Format(0, ".")
  270. End Sub
  271. ' Click event procedure for number keys (0-9).
  272. ' Append new number to the number in the display.
  273. Private Sub Number_Click(Index As Integer)
  274.     If LastInput <> "NUMS" Then
  275.         ReadOut = Format(0, ".")
  276.         DecimalFlag = False
  277.     End If
  278.     If DecimalFlag Then
  279.         ReadOut = ReadOut + Number(Index).Caption
  280.     Else
  281.         ReadOut = Left(ReadOut, InStr(ReadOut, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")
  282.     End If
  283.     If LastInput = "NEG" Then ReadOut = "-" & ReadOut
  284.     LastInput = "NUMS"
  285. End Sub
  286. ' Click event procedure for operator keys (+, -, x, /, =).
  287. ' If the immediately preceeding keypress was part of a
  288. ' number, increments NumOps. If one operand is present,
  289. ' set Op1. If two are present, set Op1 equal to the
  290. ' result of the operation on Op1 and the current
  291. ' input string, and display the result.
  292. Private Sub Operator_Click(Index As Integer)
  293.     TempReadout = ReadOut
  294.     If LastInput = "NUMS" Then
  295.         NumOps = NumOps + 1
  296.     End If
  297.     Select Case NumOps
  298.         Case 0
  299.         If Operator(Index).Caption = "-" And LastInput <> "NEG" Then
  300.             ReadOut = "-" & ReadOut
  301.             LastInput = "NEG"
  302.         End If
  303.         Case 1
  304.         Op1 = ReadOut
  305.         If Operator(Index).Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then
  306.             ReadOut = "-"
  307.             LastInput = "NEG"
  308.         End If
  309.         Case 2
  310.         Op2 = TempReadout
  311.         Select Case OpFlag
  312.             Case "+"
  313.                 Op1 = CDbl(Op1) + CDbl(Op2)
  314.             Case "-"
  315.                 Op1 = CDbl(Op1) - CDbl(Op2)
  316.             Case "X"
  317.                 Op1 = CDbl(Op1) * CDbl(Op2)
  318.             Case "/"
  319.                 If Op2 = 0 Then
  320.                    MsgBox "Can't divide by zero", 48, "Calculator"
  321.                 Else
  322.                    Op1 = CDbl(Op1) / CDbl(Op2)
  323.                 End If
  324.             Case "="
  325.                 Op1 = CDbl(Op2)
  326.             Case "%"
  327.                 Op1 = CDbl(Op1) * CDbl(Op2)
  328.             End Select
  329.         ReadOut = Op1
  330.         NumOps = 1
  331.     End Select
  332.     If LastInput <> "NEG" Then
  333.         LastInput = "OPS"
  334.         OpFlag = Operator(Index).Caption
  335.     End If
  336. End Sub
  337. ' Click event procedure for percent key (%).
  338. ' Compute and display a percentage of the first operand.
  339. Private Sub Percent_Click()
  340.     ReadOut = ReadOut / 100
  341.     LastInput = "Ops"
  342.     OpFlag = "%"
  343.     NumOps = NumOps + 1
  344.     DecimalFlag = True
  345. End Sub
  346.